package com.examples.springboot.jpa.gui.climbers;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.TableCellEditor;

import com.examples.springboot.jpa.pojos.ClimberDto;
import com.examples.springboot.jpa.pojos.MountVo;
import com.examples.springboot.jpa.resources.ClimberService;
import com.examples.springboot.jpa.resources.MountService;

@SuppressWarnings("serial")
public class ClimberListView extends JPanel implements PropertyChangeListener,ActionListener{
  private final JTable table = new JTable();
  private final ClimberTableModel tableModel;
  private final DataLoader loader;
  private JPopupMenu popup;

  public ClimberListView(ClimberService climberService,MountService mountService) {super(new GridLayout(1,0));
  List<ClimberDto> all = climberService.getAll();
  for (ClimberDto dto 
      : all) {
  System.out.println(dto);
}
  
    tableModel = new ClimberTableModel(all);
    table.setModel(tableModel);
    table.setDefaultRenderer(MountVo.class, new MountCellRenderer());
    table.setDefaultEditor(MountVo.class, new MountCellEditor(mountService.getAll(),this));
    table.setRowHeight(25);

  //Create the scroll pane and add the table to it.
    add(new JScrollPane(table,
        JScrollPane.VERTICAL_SCROLLBAR_NEVER,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

    this.loader=new DataLoader(climberService);
    this.createMenu();

    // Add the right click event.
    table.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger())
            {
                JTable source = (JTable)e.getSource();
                int row = source.rowAtPoint( e.getPoint() );
                int col = source.columnAtPoint( e.getPoint() );

                if(row>-1) {
                if (! source.isRowSelected(row))
                    source.changeSelection(row, col, false, false);

                popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
      }
    });

  }


  private void onDataPrepared(List<ClimberDto> climbers) {
//    TableCellEditor defaultEditor = table.getDefaultEditor(MountVo.class);
//    if(defaultEditor instanceof MountCellEditor)
//    {
//      ((MountCellEditor)defaultEditor).loadData();
//    }
//  this.tableModel.setData(climbers);
//  table.setModel(tableModel);
  }

  private void createMenu() {
    JMenuItem menu = new JMenuItem("Details");
    menu.setPreferredSize(new Dimension(80, 25));
    menu.addActionListener(this);
    popup = new JPopupMenu();
    popup.add(menu);
  }

  @Override
  public void propertyChange(PropertyChangeEvent evt) {
    System.out.println(evt);
    if("onComboboxChanged".equals(evt.getPropertyName()))
    {
      TableCellEditor defaultEditor = table.getDefaultEditor(MountVo.class);
      if(defaultEditor instanceof MountCellEditor)
      {
        MountVo mount = (MountVo) tableModel.getValueAt(((MountCellEditor)defaultEditor).getSelectRowNumber(), 2);
        System.out.println(evt.getNewValue());
        System.out.println(((MountCellEditor)defaultEditor).getSelectRowNumber());
      }
    }else if("postInit".equals(evt.getPropertyName()))
    {
//        loader.execute();
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    Component c = (Component)e.getSource();
    JPopupMenu popup = (JPopupMenu) c.getParent();
    JTable table = (JTable) popup.getInvoker();
    System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn());
  }


  class DataLoader extends SwingWorker<List<ClimberDto>, Void> {
    final ClimberService service;

    public DataLoader(ClimberService climberService) {
      this.service = climberService;
    }

    @Override
    protected List<ClimberDto> doInBackground() throws Exception {
      return service.getAll();
    }
    @Override
    protected void done() {
      try {
        List<ClimberDto> data = get();
        onDataPrepared(data);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }
}
